package com.zyy.Household_expenditure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyy.Household_expenditure.common.exceptionhandler.MyException;
import com.zyy.Household_expenditure.entity.HomeUser;
import com.zyy.Household_expenditure.mapper.HomeUserMapper;
import com.zyy.Household_expenditure.service.HomeUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyy.Household_expenditure.utils.JwtUtils;
import com.zyy.Household_expenditure.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class HomeUserServiceImpl extends ServiceImpl<HomeUserMapper, HomeUser> implements HomeUserService {

    //===================================================================登录
    @Override
    public String login(HomeUser user) {

        if (StringUtils.isEmpty(user.getUsername()) ||
                StringUtils.isEmpty(user.getPassword())) {
            throw new MyException(20000, "请先填写用户名，密码");
        }
        QueryWrapper<HomeUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        HomeUser userOne = baseMapper.selectOne(wrapper);
        if (user == null) {
            throw new MyException(20000, "无此用户");
        }
        if (user.getPassword() != user.getPassword()) {
            throw new MyException(20000, "密码错误");
        }
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(userOne.getId(), userOne.getUsername());
        return jwtToken;

    }

    ////=================================================================== 注册
    @Override
    public String register(HomeUser user) {

        String username = user.getUsername();
        String password = user.getPassword();
        //校验参数
        if (StringUtils.isEmpty(username) ||
                StringUtils.isEmpty(password)) {
            throw new MyException(20000, "请先填写完整手机号，密码,验证码");
        }
        //        数据添加数据库中
        //        MD加密 32位
        user.setPassword(MD5.encrypt(password));
        user.setUsername(user.getUsername());
        int insert = baseMapper.insert(user);
        String msg =null;
if (insert>0){
    msg="ok";
}
        return msg;
    }
}
